<template>
  <el-table :header-cell-style="{background:'rgba(238, 238, 238,.5)'}" :data="records" border v-loading="loading">
    <el-table-column type="index" label="序号" width="80" align="center" :index="getIndex"></el-table-column>
    <el-table-column align="center" label="名称" width="180" prop="skuName"></el-table-column>
    <el-table-column align="center" label="描述" prop="skuDesc"></el-table-column>
    <el-table-column align="center" label="默认图片" width="180" prop="skuName">
      <template v-slot="{row}">
        <img style="height: 100px;width: 100px" :src="row.skuDefaultImg">
      </template>
    </el-table-column>
    <el-table-column align="center" label="重量" width="80" prop="weight"></el-table-column>
    <el-table-column align="center" label="价格" width="80" prop="price"></el-table-column>
    <el-table-column align="center" label="操作">
      <template v-slot="{row}">
        <!--0表示当前为下架状态；1表示当前为上架状态-->
        <el-button size="small" type="success" v-if="row.isSale==1" @click="doXiaJia(row)">
          状态:已上架
        </el-button>
        <el-button size="small" type="warning" v-else @click="doShangJia(row)">
          状态:已下架
        </el-button>
        <el-button size="small" type="primary" @click="doEdit">
          <el-icon>
            <EditPen/>
          </el-icon>
        </el-button>
        <el-button size="small" type="info" @click="getSkuInfo(row)">
          <el-icon>
            <WarningFilled/>
          </el-icon>
        </el-button>
        <el-button size="small" type="danger">
          <el-icon>
            <Delete/>
          </el-icon>
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <!--分页器-->
  <el-pagination
      style="margin-top: 10px"
      background
      pager-count="7"
      :current-page="this.page"
      :page-size="this.limit"
      :page-sizes="[3, 5, 10]"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="this.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange">
  </el-pagination>
  <!--抽屉-->
  <el-drawer v-model="drawer" :show-close="false" size="40%" :with-header="false">
    <el-row>
      <el-col :offset="2" :span="5"><span style="font-weight: bold">名称：</span></el-col>
      <el-col :span="15">{{ skuInfo.skuName }}</el-col>
    </el-row>
    <el-row>
      <el-col :offset="2" :span="5"><span style="font-weight: bold">描述：</span></el-col>
      <el-col :span="15">{{ skuInfo.skuDesc }}</el-col>
    </el-row>
    <el-row>
      <el-col :offset="2" :span="5"><span style="font-weight: bold">价格：</span></el-col>
      <el-col :span="15">{{ skuInfo.price }}元</el-col>
    </el-row>
    <el-row>
      <el-col :offset="2" :span="5"><span style="font-weight: bold">平台属性：</span></el-col>
      <el-col :span="15">
        <el-tag style="margin-right: 10px;margin-bottom: 10px" type="success" v-for="attr in skuInfo.skuAttrValueList"
                :key="attr">
          {{ attr.attrId }}-{{ attr.valueId }}
        </el-tag>
      </el-col>
    </el-row>
    <el-row>
      <el-col :offset="2" :span="5"><span style="font-weight: bold">商品图片：</span></el-col>
      <el-col :span="15">
        <el-carousel :interval="1000" v-if="skuInfo.skuImageList">
          <el-carousel-item v-for="item in skuInfo.skuImageList " :key="item">
            <img style="height: 300px;width: 300px" :src="item.imgUrl">
          </el-carousel-item>
        </el-carousel>
      </el-col>
    </el-row>
  </el-drawer>
</template>

<script>
import {reqOnSale, reqSkuById, reqSkuLists} from "../../api/realRequests";
import {ElMessage} from "element-plus";

export default {
  name: "Sku",
  data() {
    return {
      // 分页数据
      page: 1,
      limit: 10,
      total: 0,
      // 用来存储服务器返回sku列表的数据
      records: [],
      // 用来存储服务器返回某条sku的数据
      skuInfo: {},
      // 控制抽屉的显隐
      drawer: false
    }
  },
  methods: {
    // 获取sku列表
    async getSkuLists() {
      let res = await reqSkuLists(this.page, this.limit)
      // console.log(res)
      if (res.code == 200) {
        this.records = res.data.records
        this.total = res.data.total
      }
    },
    // 计算并获取表格序号
    getIndex(index) {
      const page = this.page
      const limit = this.limit
      return (page - 1) * limit + index + 1
    },
    // 页码变化时
    handleCurrentChange(val) {
      this.page = val
      this.getSkuLists()
    },
    // 页码规格发生切换时
    handleSizeChange(val) {
      this.limit = val
      this.getSkuLists()
    },
    // 上架
    async doShangJia(row) {
      let res = await reqOnSale(row.id)
      // console.log(res)
      if (res.code == 200) {
        // 请求成功，说明后端数据真的改变了，我再改前端按钮的显示的上下架状态
        // 1表示当前为上架状态
        row.isSale = 1
        ElMessage({message: '上架成功！', type: 'success', duration: 2000, center: 'true'})
      }
    },
    // 下架
    async doXiaJia(row) {
      let res = await reqOnSale(row.id)
      // console.log(res)
      if (res.code == 200) {
        // 请求成功，说明后端数据真的改变了，我再改前端按钮的显示的上下架状态
        // 0表示当前为下架状态
        row.isSale = 0
        ElMessage({message: '下架成功！', type: 'warning', duration: 2000, center: 'true'})
      }
    },
    // 编辑按钮
    doEdit() {
      ElMessage({message: '正在开发中...', duration: 2000, center: 'true'})
    },
    // 获取当前sku的信息
    async getSkuInfo(row) {
      // 打开抽屉
      this.drawer = true
      let res = await reqSkuById(row.id)
      // console.log(res)
      if (res.code == 200) {
        this.skuInfo = res.data
      }
    }
  },
  mounted() {
    // 挂载完成后，立刻查询一次列表数据
    this.getSkuLists()
  }
}
</script>

<style scoped>
.el-row {
  margin-bottom: 20px;
}

/*轮播图底部指示器样式*/
/deep/ .el-carousel__button {
  background-color: red;
  height: 15px;
  width: 15px;
  border-radius: 50%;
}
</style>